<?php

declare (strict_types=1);
namespace App\Model\admin;

use Hyperf\DbConnection\Model\Model;
use HyperfExt\Auth\Authenticatable;
use HyperfExt\Auth\Contracts\AuthenticatableInterface;
use HyperfExt\Jwt\Contracts\JwtSubjectInterface;

/**
 * 系统用户模型
 *
 * @property int $id ID
 * @property string $role_ids 角色权限ID
 * @property string $head_img 头像
 * @property string $username 用户登录名
 * @property string $password 用户登录密码
 * @property string $salt 盐值
 * @property string $mobile 联系手机号
 * @property string $remark 备注说明
 * @property int $login_num 登录次数
 * @property int $sort 排序
 * @property int $status 状态(0:禁用,1:启用,)
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property \Carbon\Carbon $deleted_at 删除时间
 */
class SystemUser extends Model implements AuthenticatableInterface ,JwtSubjectInterface
{
    use Authenticatable;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'system_user';

    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'role_ids', 'head_img', 'username', 'nickname', 'password', 'salt', 'mobile', 'remark', 'login_num', 'sort', 'status', 'created_at', 'updated_at', 'deleted_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'login_num' => 'integer', 'sort' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime'];

    /**
     * 访问器
     * @param $value
     * @return false|string[]
     */
    public function getRoleIdsAttribute($value)
    {
        if ($value)
            return explode(',',$value);
        return null;
    }

    /**
     * 修改器
     * @param $value
     */
    public function setRoleIdsAttribute($value)
    {
        $this->attributes['role_ids'] = join(',',$value);
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJwtIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     */
    public function getJwtCustomClaims(): array
    {
        return ['guard' => 'admin'];
    }

    /**
     * 保存用户信息
     * @param $data
     * @return bool|string[]
     */
    public function saveSystemUser($data){
        if (!empty($data['id']))
            $map[] = ['id','<>',$data['id']];

        $map[] = ['username','=',$data['username']];

        if($this->where($map)->exists()){
            return ['msg'=>'用户名重复'];
        }

        $data['salt'] = uniqid();
        $data['password'] = password_hash($data['password'].$data['salt'], PASSWORD_DEFAULT);
        if (!empty($data['id'])){
            $info = $this->find($data['id']);
            $data['salt'] = $info['salt'];
            if (!empty($data['password']))
                $data['password'] = password_hash($data['password'].$info['salt'], PASSWORD_DEFAULT);
            else
                unset($data['password']);

            $res = $this->where('id',$data['id'])->update($data);
        }else{
            $res = $this->create($data);
        }
        if ($res){
            return true;
        }
        return ['errMsg'=>'保存失败'];
    }
}